package com.takeshequ.feeds;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.takeshequ.Constants;
import com.takeshequ.R;
import com.takeshequ.chat.ExpressionUtil;
import com.utils.HttpClientUtils;
import com.utils.HttpClientUtils.HttpResponseHandler;

public class ContentItem implements ListItems {

	private FeedsListItem feed;
	private String tag;

	private class ViewHolder {
		public TextView nickName, content, distance, time, loveNumber,
				chatNumber, age, sourceFrom;
		public ImageView headImage, picImage, genderImage,loveImage;
	}

	public ContentItem(FeedsListItem item) {
		feed = item;
	}

	public FeedsListItem getFeed() {
		return feed;
	}

	public void setFeed(FeedsListItem feed) {
		this.feed = feed;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	@Override
	public int getLayout() {
		return R.layout.feeds_detail_feed;
	}

	@Override
	public boolean isClickable() {
		return false;
	}

	@Override
	public View getView(final Context context, View convertView,
			LayoutInflater inflater) {
		convertView = inflater.inflate(getLayout(), null);
		final ViewHolder holder = new ViewHolder();
		holder.picImage = (ImageView) convertView
				.findViewById(R.id.feeds_list_view_pic);
		holder.nickName = (TextView) convertView
				.findViewById(R.id.feeds_list_view_nickName);
		holder.nickName.getPaint().setFakeBoldText(true);
		holder.headImage = (ImageView) convertView
				.findViewById(R.id.feeds_list_view_head_img);
		holder.distance = (TextView) convertView
				.findViewById(R.id.feeds_list_item_distance);
		holder.content = (TextView) convertView
				.findViewById(R.id.feeds_list_view_content);
		holder.time = (TextView) convertView
				.findViewById(R.id.feeds_list_view_time);
		holder.age = (TextView) convertView.findViewById(R.id.feeds_item_age);
		holder.genderImage = (ImageView) convertView
				.findViewById(R.id.fedds_item_gender_img);
		holder.chatNumber = (TextView) convertView
				.findViewById(R.id.feeds_list_item_chat_number);
		holder.loveNumber = (TextView) convertView
				.findViewById(R.id.feeds_list_item_love_number);
		
		holder.loveImage =(ImageView)convertView.findViewById(R.id.feeds_list_view_love_btn);
		
		
		
		holder.sourceFrom = (TextView) convertView
				.findViewById(R.id.feeds_list_item_source);
		holder.nickName.setText(feed.getNickName());
		
		
		
		FinalBitmap.create(context).display(holder.headImage,
				feed.getHeadImage());

		SpannableString spannableString = ExpressionUtil
				.getExpressionString(context, feed.getContPreview());
		holder.content.setText(spannableString);
		holder.distance.setText(feed.getDistance() + " KM");
		holder.time.setText(feed.getCreateDateString());
		FinalBitmap.create(context).display(holder.picImage,
				feed.getImage());
		if (feed.getSex() instanceof String) {
			if (feed.getSex().equals("m")) {
				holder.genderImage.setBackgroundDrawable(context.getResources()
						.getDrawable(R.drawable.friends_item_male));
			} else {
				holder.genderImage.setBackgroundDrawable(context.getResources()
						.getDrawable(R.drawable.friends_item_female));
			}
		}
		holder.age.setText(feed.getAge());
		holder.chatNumber.setText(feed.getCommentCount());
		holder.loveNumber.setText(feed.getPraiseCount());
		holder.sourceFrom.setText(feed.getSourcefrom());
		holder.loveImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Map<String, String> params = new HashMap<String, String>();
				params.put("memberId", Constants.memberId);
				params.put("nickName", Constants.member.getNickName());
				params.put("headImage", Constants.member.getHeadImage());
				params.put("behaviorId", feed.getBehaviorId());
				params.put("cont", "");
				params.put("commentType", "2");
				HttpClientUtils.post("/praiseBehavior", params,
						new HttpResponseHandler() {

							@Override
							public void handler(String response) {
								try {
									JSONObject jsonObject = new JSONObject(
											response);
									holder.loveNumber.setText(jsonObject
											.getString("totalCount"));
									Toast.makeText(context, "已赞", 2000)
											.show();
								} catch (JSONException e) {
									e.printStackTrace();
								}

							}
						});
			}
		});
		
		return convertView;
	}
}
